#include "hy_reactor.h"

#ifdef _WIN32
#include "hy_iocp_reactor.h"
#else
#include "hy_epoll_reactor.h"
#endif

hy_reactor::hy_reactor( hy_u32_t threads /*= 1*/, hy_reactor_type rt /*= hrt_fast*/ ) 
:_type(rt),_thread(NULL),_reactor(NULL)
{
	//switch (rt)
	//{
	//case hrt_fast:
#ifdef _WIN32
		_reactor = new hy_iocp_reactor();
#else
		_reactor = new hy_epoll_reactor();
#endif
		//break;
	//default:
	//	break;
	//}
}

hy_reactor::~hy_reactor()
{
	if (_reactor != NULL)
	{
		_reactor->stop();
		delete _reactor;
	}
	if (_thread != NULL)
	{
		_thread->join();
		delete _thread;
	}
}

hy_err_t hy_reactor::init()
{
	return _reactor->init();
}

void hy_reactor::unit()
{
	_reactor->unit();
}

void hy_reactor::run()
{
	//hy_assert(_reactor != NULL);
	hy_assert(_thread == NULL);
	void* param = NULL;
	_thread = new hy_thread(bind_thread(&hy_reactor_base::_run,_reactor,param));
}

void hy_reactor::stop()
{
	_reactor->stop();
	_thread->join();
}

hy_err_t hy_reactor::checkin( hy_handle_base* hb )
{
	return _reactor->checkin(hb);
}

hy_err_t hy_reactor::checkout( hy_handle_base* hb )
{
	return _reactor->checkout(hb);
}

hy_err_t hy_reactor::send_notify( hy_handle_base* hb )
{
	return _reactor->send_notify(hb);
}

hy_err_t hy_reactor::recv_notify( hy_handle_base* hb )
{
	return _reactor->recv_notify(hb);
}
